package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.pojo.vo.FormDataAddGetVO;
import com.xbongbong.paas.pojo.vo.PaasFormDataEsListVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.thread.TransmittableThreadLocalUtil;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.bom.pojo.dto.BomBillListDTO;
import com.xbongbong.pro.businessproduct.pojo.vo.BusinessProductListVO;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.detailtab.dto.DetailTabDataGetDTO;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.simulationproduce.pojo.dto.ProductionOrderAddGetDTO;
import com.xbongbong.saas.service.BomBillService;
import com.xbongbong.saas.service.BusinessProductService;
import com.xbongbong.saas.service.ProductionOrderService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 模拟生产
 * @author 吴峰
 * @version v1.0
 * @date 2019/4/23 17:38
 * @since v1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/simulationProduce")
public class SimulationProduceController {

    @Resource
    private BomBillService bomBillService;
    @Resource
    private BusinessProductService businessProductService;
    @Resource
    private ProductionOrderService productionOrderService;

    /**
     * 选择bom
     * @author wufeng
     * @date 2018/8/29 16:21
     * @param bomBillListDTO
     * @param br
     * @descption
     * @return java.lang.String
     */
    @RequestMapping(value = "/bomBill/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid BomBillListDTO bomBillListDTO, BindingResult br) throws Exception {
        XbbResponse<PaasFormDataEsListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(bomBillListDTO.getSaasMark(), bomBillListDTO.getBusinessType(), null, bomBillListDTO.getDistributorMark());
                PaasFormDataEsListVO paasFormDataEsListVO = bomBillService.list(bomBillListDTO);
                response = new XbbResponse<>(paasFormDataEsListVO);
            } catch (XbbException e){
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 开始模拟
     * @author 吴峰
     * @date 2019/4/22 9:22
     * @param
     * @return java.lang.String
     * @throws XbbException 业务异常
     */
    @RequestMapping(value = "/bomBill/bomBillProductList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String bomBillProductList(@RequestBody @Valid DetailTabDataGetDTO detailTabDataGetDTO, BindingResult br) throws Exception {
        XbbResponse<BusinessProductListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(detailTabDataGetDTO.getSaasMark(), detailTabDataGetDTO.getBusinessType(), null, detailTabDataGetDTO.getDistributorMark());
                BusinessProductListVO businessProductListVO = businessProductService.getBusinessProductList(detailTabDataGetDTO);
                response = new XbbResponse<>(businessProductListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/produce", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String produce(@RequestBody @Valid ProductionOrderAddGetDTO productionOrderAddGetDTO, BindingResult br) throws Exception {
        XbbResponse<FormDataAddGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FormDataAddGetVO formDataAddGetVO = productionOrderService.produce(productionOrderAddGetDTO);
                response = new XbbResponse<>(formDataAddGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


}
